﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Goblinfactory.NMenu.Framework;
using Goblinfactory.SpecUtils;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace NMenu.Specs
{
    [Binding]
    public class DisplayVerticalMenuSteps
    {

        private Menu _menu;
        private HorizontalMenuView _view;


        [Given(@"a menu with Exit text of ""(.*)"" and menu width of (\d+) and fruit options of")]
        public void GivenAMenuOfFruitOptions(string quitText, int width, Table table)
        {
            string[] fruitOptions = table.GetColumn("Fruit");
            _menu = new Menu(fruitOptions, width, quitText);
        }

        [Given(@"a menu with fruit options of")]
        public void GivenAMenuOfFruitOptions(Table table)
        {
            string[] fruitOptions = table.GetColumn("Fruit");
            _menu = new Menu(fruitOptions,null,null);
        }


        [When(@"I view the menu")]
        public void WhenIViewTheMenu()
        {
            _view = _menu.GetHorizontalMenu();
        }

        [Then(@"the option numbers should be")]
        public void ThenTheOptionNumbersShouldBe(Table table)
        {
            int i = 0;
            foreach (var option in _view.Options)
            {
                var row = table.Rows[i];
                Assert.That(option.Number, Is.EqualTo(int.Parse(row["Sequence"])));
                i++;
            }
        }

        [Then(@"the menu titles should be")]
        public void ThenTheMenuTitlesShouldBe(Table table)
        {
            int i = 0;
            foreach (var option in _view.Options)
            {
                var row = table.Rows[i];
                Assert.That(option.Title, Is.EqualTo(row["Title"]));
                i++;
            }
        }


        [Given(@"a menu with exit text of ""(.*)""")]
        public void GivenAMenuWithExitText(string exitText )
        {
            _menu = new Menu(new string[] {},null, exitText);
        }

        [Given(@"a menu")]
        public void GivenAMenu()
        {
            _menu = new Menu(new string[] { }, null, null);
        }


        [Then(@"there should be only 1 exit menu")]
        public void ThenThereShouldBeOnly1ExitMenu()
        {
            Assert.IsNotNull(_view);
            Assert.IsNotNull(_view.Options);
            var exitOptions = _view.Options.OfType<ExitMenuOption>();
            Assert.AreEqual(1,exitOptions.Count());
        }

        	
        [Then(@"I should see the text ""(.*)"" next to the exit menu.")]
        public void ThenIShouldSeeTheTextNextToTheExitMenu(string exitText)
        {
            Assert.IsNotNull(_view);
            Assert.IsNotNull(_view.Options);
            var exitOptions = _view.Options.OfType<ExitMenuOption>();
            Assert.AreEqual(exitText, exitOptions.ElementAt(0).Title);
        }

        [Then(@"the last option should be a quit option")]
        public void ThenTheLastOptionShouldBeAQuitOption()
        {
            Assert.IsNotNull(_view);
            Assert.IsNotNull(_view.Options);
            var last = _view.Options.Last();
            Assert.That(last, Is.InstanceOf<ExitMenuOption>());
        }




    } // class
} // namespace
